<?php
class TagsController extends AppController {

	var $name = 'Tags';

	function index() {
		$this->Tag->bindModel(array(
											 'hasAndBelongsToMany' => array(
													'Task' =>
														array('className' => 'Task',
																'joinTable' => 'tags_tasks',
																'foreignKey' => 'tag_id',
																'associationForeignKey' => 'task_id',
																'conditions' => 'Task.types_id != ' . $this->settings['task_type_finished']))));
		$tags = $this->Tag->find('all');
		$this->set('tags', $tags);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tag', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tag', $this->Tag->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Tag->create();
			if ($this->Tag->save($this->data)) {
				$this->Session->setFlash(__('The Tag has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Tag could not be saved. Please, try again.', true));
			}
		}
		$tasks = $this->Tag->Task->find('list');
		$this->set(compact('tasks'));
	}

	function edit($id = null) {
		if(!empty($this->params['form']['value'])) {
			if(!$this->Tag->save(
									array(
										'Tag' => array(
														'id' => $this->params['form']['editorId'],
														'tag' => $this->params['form']['value'])))){
				$tag = __('The Tag could not be saved. Please, try again.', true);
			}
		}
		$this->Tag->unbindModel(array('hasAndBelongsToMany' => array('Task')));
		$tag = $this->Tag->read(null, $id);
		$this->set('tag', $tag['Tag']['tag']);
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tag', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Tag->del($id)) {
			$this->Session->setFlash(__('Tag deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Tag could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}
	
/*
 * ajaxos keresés a címkékben
*/
	function search(){
		$this->Task->recursive = 1;
		$this->set('searchResults', $this->Tag->find('all',
																	array(
																		'conditions' => "tag LIKE '%{$this->data['Task']['TaskTags']}%'",
																		'limit' => 5
																		)
																  )
					 );
		$this->render('searchResults');
	}
	
/*
 * adott cimkéhez tartozó feladatok megjelenítése
 * @param int $id a cimke id-je
*/
	function lista($id = null){
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tag.', true));
			$this->redirect('/');
		}
		$this->Tag->unbindModel(array('hasAndBelongsToMany' => array('Task')));
		$this->Tag->bindModel(array('hasAndBelongsToMany' => array(
																							'Task' => array(
																											'className' => 'Task',
																											'joinTable' => 'tags_tasks',
																											'foreignKey' => 'tag_id',
																											'associationForeignKey' => 'task_id',
																											'conditions' => 'Task.types_id != ' . $this->settings['task_type_finished'],
																											'fields' => array('Task.id'),
																											'order' => '',
																											'limit' => '',
																											'offset' => '',
																											'unique' => '',
																											'finderQuery' => '',
																											'deleteQuery' => '',
																											'insertQuery' => ''
																											)
																							)
													)
											);
		$tag = $this->Tag->find('Tag.id = ' . $id);
		$this->set('tag', $tag);
		$this->render('lista');
	}

}
?>